package mn.slide.laborberlin.parse;

import java.io.IOException;
import java.util.ArrayList;

import mn.slide.laborberlin.main.R;
import mn.slide.laborberlin.model.PListInnerModel;
import mn.slide.laborberlin.model.PListMainModel;
import mn.slide.laborberlin.model.PlistModel;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;

public class ParsePlistLevelUp {

	int i = 0;
	ArrayList<PlistModel> childList;
	ArrayList<PListInnerModel> innerList;
	ArrayList<PListMainModel> mainList;
	PlistModel child;
	PListInnerModel inner;
	PListMainModel main;
	Activity activity;
	String key;
	String value;

	public ParsePlistLevelUp(Activity activity) {
		this.activity = activity;
	}

	public ArrayList<PListMainModel> getEventsFromAnXML()
			throws XmlPullParserException, IOException {
		Resources res = activity.getResources();
		XmlResourceParser xpp = res.getXml(R.layout.plist);
		xpp.next();
		int eventType = xpp.getEventType();
		int flag1 = 0, flag2 = 0, flag3 = 0, flag4 = 0;
		while (eventType != XmlPullParser.END_DOCUMENT) {

			if (eventType == XmlPullParser.START_TAG) {

				String mainTag = xpp.getName().toString();
				if (mainTag.equals("dict")) {
					if (flag1 == 0) {
						flag1 = 1;
						i = i + 1;
						mainList = new ArrayList<PListMainModel>();
					} else if (flag2 == 0) {
						// inner = new PListInnerModel();
						// stringBuffer
						// .append("\n\n------------INNER DICTIONARY STARTS HERE----------\n");
						innerList = new ArrayList<PListInnerModel>();
						flag2 = 1;
					} else {
						// stringBuffer
						// .append("\n\n------------CHILD DICTIONARY STARTS HERE----------\n");
						flag4 = 1;
					}

				}

				else if (mainTag.equals("key")) {
					flag3 = 1;
				} else {
					flag3 = 2;
				}
			} else if (eventType == XmlPullParser.END_TAG) {
				String g = xpp.getName().toString();

				if (g.equals("dict")) {
					if (flag1 == 1 && flag2 != 1 && flag4 != 1) {

						// stringBuffer
						// .append("\n\n------------MAIN DICTIONARY ENDS HERE----------\n\n\n");
						flag1 = 0;
					} else if (flag1 == 1 && flag2 == 1 && flag4 != 1) {
						// stringBuffer
						// .append("\n\n-----INNER DICTIONARY ENDS----------\n");
						main.setChild(innerList);
						mainList.add(main);
						flag2 = 0;
					} else {
						inner.setChild(child);
						innerList.add(inner);

						// stringBuffer
						// .append("\n\n-----CHILD DICTIONARY ENDS----------\n");

						flag4 = 0;
					}
				}
			} else if (eventType == XmlPullParser.TEXT) {
				if (flag3 == 1) {
					key = xpp.getText();
					if (flag1 == 1 && flag2 != 1 && flag4 != 1) {
						main = new PListMainModel();
						main.setValue(key);
					} else if (flag1 == 1 && flag2 == 1 && flag4 != 1) {
						inner = new PListInnerModel();
						inner.setValue(key);
						child = new PlistModel();
					}
					// stringBuffer.append("\nkey:=" + key);
					flag3 = 0;
				} else if (flag3 == 2) {
					value = xpp.getText();
					child.addChild(key, value);
					// stringBuffer.append("\nValue:=" + xpp.getText());
					flag3 = 0;
				}
			}

			eventType = xpp.next();
		}
		// stringBuffer.append("\n--- End XML ---");
		// stringBuffer.append("\n--- Total Number Of Dictionaries Are ---" +
		// i);

		return this.mainList;
	}
}
